package com.sunnypower.rushfitapp;


import android.os.Bundle;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import com.sunnypower.rushfitapp.Noticepart.NoticeListAdapter;
import com.sunnypower.rushfitapp.entity.Notice;
import com.sunnypower.rushfitapp.util.GetData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class NoticeActivity extends AppCompatActivity {

    private List<Notice> mNoticeBeanList;
    private NoticeListAdapter adapter;
    private ListView lv_notice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);

        mNoticeBeanList = new ArrayList<>();
        try {
            initCoachData();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        lv_notice = findViewById(R.id.lv_notice);
        adapter = new NoticeListAdapter(NoticeActivity.this, mNoticeBeanList);
        lv_notice.setAdapter(adapter);
    }

    private void initCoachData() throws JSONException {
        String data = GetData.doGet("http://120.24.24.141:10990/rushfit/FindAllNotice2app");
        System.out.println(data);
        //解析json数据
        System.out.println("responseData：" + data);
        JSONArray NoticeList = null;
        try {
            NoticeList = new JSONArray(data);
            System.out.println("NoticeList：" + NoticeList);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        for (int i = 0; i < NoticeList.length(); i++) {
            JSONObject jsonCoach = NoticeList.getJSONObject(i);
            Notice bean = new Notice();
            bean.setTitle(jsonCoach.getString("title"));
            bean.setContext(jsonCoach.getString("context"));
            mNoticeBeanList.add(bean);
        }
    }
}
